
import { getPayAppId } from '@/service/api/pay'
const state = {
  payResolve: null,
  payReject: null
};

const mutations = {
  SET_PAY_RESULT(state, result = {}) {
    state.payResolve = result.resolve
    state.payReject = result.reject
  }
};
const actions = {
  
  async pay({ commit }, publicInfo) {
    try {
      const {appId = ''} = await getPayAppId({storeNo: publicInfo.storeNo})
      if(!appId) return new Promise((resolve, reject) => reject({code: -1, msg: '参数错误'}))
      commit('SET_PAY_RESULT')
      return new Promise((resolve, reject) => {
        const { fail = null } = publicInfo
        wx.navigateToMiniProgram({
          appId: appId,
          path: '/pages/publicPay/publicPay?xhOrderNo=' + publicInfo.xhOrderNo,
          extraData: {publicInfo},
          envVersion: 'trial',
          success: () => commit('SET_PAY_RESULT', {resolve, reject}),
          fail: () => typeof fail === 'function' && fail()
      })
    })
    } catch (error) {console.error(error)}
    
    
  }
};

const getters = {
  payResolve: state => state.payResolve
};

export default {
  namespaced: true,
  state,
  actions,
  getters,
  mutations,
};
